package com.heisenhub.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heisenhub.system.entity.Menu;
import com.heisenhub.system.entity.User;
import com.heisenhub.system.mapper.UserMapper;
import com.heisenhub.system.service.IMenuService;
import com.heisenhub.system.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author sen
 * @since 2023-12-24
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    IMenuService menuService;
    
    @Override
    public User findByName(String username) {
        log.info("【查找登入用户】  {}",username);
        User user = this.baseMapper.findByName(username);
        if(null != user) {
            String userPermissions = findUserPermissions(username);
            user.setPermissions(userPermissions);
        }
        return user;
    }

    @Override
    public String findUserPermissions(String username) {
        log.info("【查找用户权限】  {}",username);
        List<Menu> userPermissions = menuService.findUserPermissions(username);
        if(CollectionUtils.isEmpty(userPermissions)) return null ;
        return userPermissions.stream().map(Menu::getPerms).collect(Collectors.joining(","));
    }

}
